package com.blockmeta.bbs.immodule.widget.msgview;

import android.content.Context;

import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;

import java.util.HashMap;
import java.util.Map;

import static com.blockmeta.bbs.immodule.widget.msgview.SuppotMsgViewType.UNSUPPORT_KEY;

public class MsgViewManager<T extends ChatMessage> implements IMsgViewManger<T>{

    private Map<Integer, IMsgView> msgViewMap;
    private Context mContext;

    public MsgViewManager(Context context) {
        mContext = context;

        SuppotMsgViewType suppotMsgViewType = new SuppotMsgViewType();
        msgViewMap = suppotMsgViewType.getSupportViews(mContext);
    }


    @Override
    public void addItemTypes(BaseMultiItemQuickAdapter chatAdapter) {
        for (Map.Entry<Integer, IMsgView> entry:
                msgViewMap.entrySet()) {
            chatAdapter.addItemType(entry.getKey(), entry.getValue().getLayoutId());
        }
    }

    @Override
    public IMsgView<ChatMessage> getMsgView(int itemViewType) {
        IMsgView iMsgView = msgViewMap.get(itemViewType);
        //如果以后添加了新的类型，为了兼容老的，需要返回默认的view
        if(iMsgView == null){
            iMsgView = new UnsupportMsgView(mContext);
        }
        return iMsgView;
    }
}
